var details = null;
var progressHistory = "";

/*
 自定义模板规则：商机信息列表
 */
Handlebars.registerHelper('oppHistory', function (items, options) {
    var itemArr = new Array();
    if (items instanceof Array) {
        itemArr = items;
    } else if (items instanceof Object) {
        itemArr.push(items);
    }

    var out = '';
    for (var i = 0, l = itemArr.length; i < l; i++) {
        var item = itemArr[i];
        out += '<table class="am-table am-table-compact am-text-sm am-table-bordered">'
            + '<tbody>';

        out += '<tr>'
            + '<td class="am-text-nowrap table-td-title">序列：</td>'
            + '<td class="am-text-secondary table-td-content">'
            + item.INDEX
            + '</td>'
            + '</tr>'
            + '<tr>'
            + '<td class="am-text-nowrap table-td-title">状态：</td>'
            + '<td class="am-text-secondary table-td-content">'
            + item.TACHE_NAME
            + '</td>'
            + '</tr>'
            + '<tr>'
            + '<td class="am-text-nowrap table-td-title">处理人：</td>'
            + '<td class="am-text-secondary table-td-content">'
            + item.DEAL_NAME
            + '</td>'
            + '</tr>'
            + '<tr>'
            + '<td class="am-text-nowrap table-td-title">工号：</td>'
            + '<td class="am-text-secondary table-td-content">'
            + item.DEAL_NO
            + '</td>'
            + '</tr>'
            + '<tr>'
            + '<td class="am-text-nowrap table-td-title">电话：</td>'
            + '<td class="am-text-secondary table-td-content">'
            + item.DEAL_PHONE
            + '</td>'
            + '</tr>'
            + '<tr>'
            + '<td class="am-text-nowrap table-td-title">备注：</td>'
            + '<td class="am-text-secondary table-td-content">';
            if(typeof item.DEAL_COMMENT == "string"){
                out += item.DEAL_COMMENT;
            }
            out += ''
            + '</td>'
            + '</tr>'
            + '<tr>'
            + '<td class="am-text-nowrap table-td-title">开始时间：</td>'
            + '<td class="am-text-secondary table-td-content">'
            + item.START_TIME
            + '</td>'
            + '</tr>'
            + '<tr>'
            + '<td class="am-text-nowrap table-td-title">结束时间：</td>'
            + '<td class="am-text-secondary table-td-content">';
            if(typeof item.FINISH_TIME == "string"){
                out += item.FINISH_TIME;
            }
            out += ''
            + '</td>'
            + '</tr>';

        out += '</tbody></table>';
    }
    console.log("html:" + out);
    return out;
});

var loadData = function () {
    var reqUrl = Constants.rpcCrmHost + "QueryPreDealStatus";
    var params = {"BODY": {"REQUEST_INFO": {"PRE_BUSI_ID": getParamValue("id")}}};
    var loadingBarId = "loadingbar";
    $.ajax({
        type: "post",
        contentType: "application/json",
        data: JSON.stringify(params),
        url: reqUrl,
        dataType: "text",
        async: true,
        headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
            if(data.indexOf("exception") !=-1){
                showLoadingBar($('#' + loadingBarId), false);
                alert("网络或服务器异常，请稍后再试！");
                console.info("request error:");
                return false;
            }
            data=data.replace(/\"PRE_BUSI_ID\":\s*([0-9]+)/,function(a,b){return  '"PRE_BUSI_ID":"'+b+'"';});
            data= $.parseJSON(data);
            showLoadingBar($('#' + loadingBarId), false);
            console.log("data = " + JSON.stringify(data));
            var returnData = data.ROOT.OUT_DATA;
            console.info(returnData);
            showData(returnData);
        },
        error: function (textStatus) {
            showLoadingBar($('#' + loadingBarId), false);
            alert("网络或服务器异常，请稍后再试！");
            console.info("request error:");
        }
    });
};

var showData = function (details) {
    if (!details) {
        alert("数据加载错误！");
        return;
    }
    $("#pre_busi_name").html(details.PRE_BUSI_INFO.PRE_BUSI_NAME);
    $("#pre_busi_id").html(details.PRE_BUSI_INFO.PRE_BUSI_ID);
    $("#pre_busi_status").html(details.PRE_BUSI_INFO.PRE_BUSI_STATUS);
    var $tpl = $('#info-tpl');
    var source = $tpl.text();
    var template = Handlebars.compile(source);
    var myData = {
        list: details.PRE_BUSI_INFO.OPER_INFO_LIST.OPER_INFO
    };

    var html = template(myData);
    $("#info").html(html);


};

$(function () {
    loadData();
    $("#submit").on("click", function () {
        /*var jump_url = "opportunity_list.html";
        if(getParams() && getParams()!=""){
            jump_url +="?"+getParams();
        }
        alert(jump_url);
        window.location.href=jump_url;*/
        window.history.go(-1);
    });
});